package com.liyunc.socket.webclient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WebServer {

    /**
     * 参考链接
     * https://zhuanlan.zhihu.com/p/66166183
     * https://www.cnblogs.com/chenpi/p/5602171.html
     */
    public static void main(String[] args) throws IOException {
        final int port = 40000;
        final String host = "localhost";
        final SocketAddress address = new InetSocketAddress(host, port);

        ExecutorService executorService =
            Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        ServerSocket server = new ServerSocket();
        server.bind(address);

        try {
            Socket client;
            while ((client = server.accept()) != null) {
                executorService.execute(new HttpThread(client));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            server.close();
        }
    }
}
